function p(a){
	console.log(a)
}
function add(ul){
	if(!ul.hasClass('anm')){
		ul.addClass('anm')
	}
}

(function(){
	//闭包
	'use strict' //使用严格模式
	// banner
	var Banner = function(container,params){
		if(!this instanceof Banner) return new Banner(container,parmas);
		var  defaults={
			direction: 'horizontal', //banner 方向
			slideClass:''+container || 'container',
			initialSlide: 0, //初始幻灯片
			speed:300,
			autoplay:false,
			autoplayDisableOnInteraction: true, //自动播放禁用交互
            autoplayStopOnLast: false,  //自动播放停止在最后
            autoHeight: false,
            setWrapperSize: false,//设置包装尺寸
            effect: 'slide',
		}
		params = params || {};
		for (var def in defaults) {
            if (typeof params[def] === 'undefined') {
                params[def] = defaults[def];
            }
            else if (typeof params[def] === 'object') {
                for (var deepDef in defaults[def]) {
                    if (typeof params[def][deepDef] === 'undefined') {
                        params[def][deepDef] = defaults[def][deepDef];
                    }
                }
            }
        }
		var s = this;
		s.params = params;
		var banner = $('.'+s.params.slideClass);
		var ul = banner.find('ul');
		var li = banner.find('li');
		var banner_width= banner.find('li').width();
		var num = banner.find('li').length;
		s.num = banner.find('li').length;
		ul.append(li.clone());		
		ul.width(banner_width*num*2);
		ul.css('left',-banner_width*num+'px');
		var page_btn = banner.siblings('span');
		var flag = true;
		var page_index=0;
		this.pagination=banner.find('.pagination');
		
		page_btn.click(function(){
			if(flag){				
				if(this.className === 'left_btn'){
			 		s.prevpage();
			 		page_index ==0?page_index=num-1:page_index--
				}else{
				 	s.nextpage();
				 	page_index ==num-1?page_index=0:page_index++
				}
				flag=false;
				setTimeout(function(){
					flag=true;
				},1000)
			}								
		})
		s.nextpage =function(){	
			ul.removeClass('anm');
			var now_left = parseInt(ul.css('left'));	
			if(now_left<=-banner_width*num){
				now_left=now_left+banner_width*num;
				ul.css('left',now_left+'px');
			}			
			setTimeout(function(){
				add(ul);
				ul.css('left',now_left-banner_width+'px');
				if(now_left-banner_width == -banner_width*num){				
					setTimeout(function(){
						ul.removeClass('anm');
						ul.css('left',0);
					},1000);
				}
			s.checkPagination()
			},1);						
		}
		s.prevpage =function(){
			ul.removeClass('anm')
			var now_left = parseInt(ul.css('left'));
			if(now_left>-banner_width*num){
				now_left=now_left-banner_width*num;
				ul.css('left',now_left+'px');
			}
			setTimeout(function(){
				add(ul)
				ul.css('left',now_left+banner_width+'px');
				if(now_left+banner_width == 0){				
					setTimeout(function(){
						ul.removeClass('anm');
						ul.css('left',-banner_width*num+'px');
					},1000);
				}
			s.checkPagination()
			},1);
			
		}

		s.appentPagination =function(){
			for(var i=0;i<this.num;i++){
				this.pagination.append('<span></span>');
			}
			this.pagination.children().eq(page_index).addClass('cur');
		}
		s.checkPagination=function(){
			this.pagination.children().eq(page_index).addClass('cur').siblings().removeClass();
		}
		s.appentPagination();
		function auto(){
			page_btn.eq(1).click()
			setTimeout(auto,s.params.speed)
		}
		if(s.params.autoplay){		
			auto()
		}
		
	}
	window.Banner = Banner;
})();
